package cn.zhc.com.utils;

import android.app.Activity;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.WindowManager;
import android.widget.EditText;

/**
 * Created by zhouhongchun on 2017/11/9.
 */

public class UtilsEditView {
    /**
     * EditText获取焦点并显示软键盘
     */
    public static void showSoftInputFromWindow(Activity activity, EditText editText) {
        initEditView(true,editText);
        activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
    }

    /**
     * initEditView 是否可输入,建议在EditView外面放一个容器，容器监听点击事件来设置EditView是否可以输入
     * @param isEditEnable
     * @param mEditText
     */
    public static void initEditView(boolean isEditEnable, EditText mEditText) {

        if (isEditEnable) {
            mEditText.setEnabled(true);
            mEditText.setFocusable(true);
            mEditText.setFocusableInTouchMode(true);
            mEditText.requestFocus();

        } else {
            mEditText.setEnabled(false);
            mEditText.setFocusableInTouchMode(false);
            mEditText.setFocusable(false);
        }

    }

    /**
     * EditView的输入控制，仅适用于数字类型
     * @param mEditText
     * @param prefixLength 整数部分长度,0表示不限制
     * @param suffixLength 小数点长度，0表示不限制
     * @param inputTotalLength 输入总长度，0表示不限制
     */
    public static void dealEditViewInputForNumber(final EditText mEditText, final int prefixLength, final int suffixLength, int inputTotalLength){
        mEditText.setInputType(InputType.TYPE_CLASS_NUMBER
                | InputType.TYPE_NUMBER_FLAG_DECIMAL);
        if (inputTotalLength != 0){
            mEditText.setFilters(new InputFilter[] { new InputFilter.LengthFilter(inputTotalLength) });
        }

        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable editable) {
                if (editable == null || TextUtils.isEmpty(editable.toString())){
                    //TODO 内容置空时的操作
                    return;
                }
                String text = editable.toString();
                // 不合法   .  00  01
                if (text.startsWith(".") || text.startsWith("00") || (text.length() == 2 && text.startsWith("0")&&!text.startsWith("0."))){
                    mEditText.setText("");
                    return;
                }
                //小数位超过suffixLength
                if (suffixLength != 0 && text.contains(".")){
                    String[] strs = text.split("\\.");

                    if (strs.length == 2 && strs[1].length() >= suffixLength){
                        text = strs[0]+"."+strs[1].substring(0,suffixLength);
                        mEditText.setText(text);
                    }
                    mEditText.setSelection(text.length());


                }
                if (prefixLength != 0){
                    //整数时，整数位不能超过prefixLength位数
                    if (!text.contains(".")){
                        if (text.length() > prefixLength){
                            text = text.substring(0,prefixLength);
                            mEditText.setText(text);
                            mEditText.setSelection(text.length());
                        }

                    }


                }

            }
        });


    }

}
